package y2015.date1027am.sax解析内部类;

import org.junit.jupiter.api.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;


public class SaxXmlParseFile {
    @Test
    public void main() throws Exception {
        String filexml = SaxXmlParseFile.class.getResource("xmlForFirstName.xml").toString();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.newSAXParser().parse(filexml, new DefaultHandler() {

            private String qName;

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                if ("text".equals(qName)) {

                    System.out.println(new String(ch, start, length));

                }
                super.characters(ch, start, length);
            }

            @Override
            public void startElement(String uri, String localName,
                                     String qName, Attributes attributes) throws SAXException {
//				传递
                forNameString(qName);

                super.startElement(uri, localName, qName, attributes);
            }

            private void forNameString(String qName) {

                this.qName = qName;

            }

        });

    }
}

